package com.lframework.xingyun.promo.impl.limitdiscount;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.promo.entity.MarketingLimitdiscount;
import com.lframework.xingyun.promo.mappers.MarketingLimitdiscountMapper;
import com.lframework.xingyun.promo.service.limitdiscount.MarketingLimitdiscountService;
import com.lframework.xingyun.promo.vo.limitdiscount.CreateMarketingLimitdiscountVo;
import com.lframework.xingyun.promo.vo.limitdiscount.QueryMarketingLimitdiscountVo;
import com.lframework.xingyun.promo.vo.limitdiscount.UpdateMarketingLimitdiscountVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MarketingLimitdiscountServiceImpl extends BaseMpServiceImpl<MarketingLimitdiscountMapper, MarketingLimitdiscount> implements MarketingLimitdiscountService {

    @Override
    public PageResult<MarketingLimitdiscount> queryPage(Integer pageIndex, Integer pageSize, QueryMarketingLimitdiscountVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MarketingLimitdiscount> page = this.getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MarketingLimitdiscount> queryList(QueryMarketingLimitdiscountVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MarketingLimitdiscount.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MarketingLimitdiscount findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增限时折扣活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateMarketingLimitdiscountVo vo) {

        MarketingLimitdiscount data = new MarketingLimitdiscount();
        data.setId(IdUtil.getId());
        data.setName(vo.getName());
        data.setBeginDate(vo.getBeginDate());
        data.setEndDate(vo.getEndDate());
        data.setCycleFlag(vo.getCycleFlag());
        data.setCycleType(vo.getCycleType());
        data.setCycleTimeGroup(vo.getCycleTimeGroup());
        data.setCycleTimeRule(vo.getCycleTimeRule());
        data.setPromDiscount(vo.getPromDiscount());
        data.setAllMemberLevel(vo.getAllMemberLevel());
        data.setMemberLevelId(vo.getMemberLevelId());
        data.setAllBuyLimit(vo.getAllBuyLimit());
        data.setCycleBuyLimit(vo.getCycleBuyLimit());
        data.setAllProduct(vo.getAllProduct());
        data.setProductId(vo.getProductId());
        data.setUseCoupon(vo.getUseCoupon());
        data.setUseFullreduction(vo.getUseFullreduction());
        data.setUseFullprice(vo.getUseFullprice());
        data.setUseFullgiftactivity(vo.getUseFullgiftactivity());
        data.setAllShop(vo.getAllShop());
        data.setShopId(vo.getShopId());

        getBaseMapper().insert(data);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改限时折扣活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateMarketingLimitdiscountVo vo) {

        MarketingLimitdiscount data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("限时折扣活动表不存在！");
        }

        LambdaUpdateWrapper<MarketingLimitdiscount> updateWrapper = Wrappers.lambdaUpdate(MarketingLimitdiscount.class)
                .set(MarketingLimitdiscount::getName, vo.getName())
                .set(MarketingLimitdiscount::getBeginDate, vo.getBeginDate())
                .set(MarketingLimitdiscount::getEndDate, vo.getEndDate())
                .set(MarketingLimitdiscount::getCycleFlag, vo.getCycleFlag())
                .set(MarketingLimitdiscount::getCycleType, vo.getCycleType())
                .set(MarketingLimitdiscount::getCycleTimeGroup, vo.getCycleTimeGroup())
                .set(MarketingLimitdiscount::getCycleTimeRule, vo.getCycleTimeRule())
                .set(MarketingLimitdiscount::getPromDiscount, vo.getPromDiscount())
                .set(MarketingLimitdiscount::getAllMemberLevel, vo.getAllMemberLevel())
                .set(MarketingLimitdiscount::getMemberLevelId, vo.getMemberLevelId())
                .set(MarketingLimitdiscount::getAllBuyLimit, vo.getAllBuyLimit())
                .set(MarketingLimitdiscount::getCycleBuyLimit, vo.getCycleBuyLimit())
                .set(MarketingLimitdiscount::getAllProduct, vo.getAllProduct())
                .set(MarketingLimitdiscount::getProductId, vo.getProductId())
                .set(MarketingLimitdiscount::getUseCoupon, vo.getUseCoupon())
                .set(MarketingLimitdiscount::getUseFullreduction, vo.getUseFullreduction())
                .set(MarketingLimitdiscount::getUseFullprice, vo.getUseFullprice())
                .set(MarketingLimitdiscount::getUseFullgiftactivity, vo.getUseFullgiftactivity())
                .set(MarketingLimitdiscount::getAllShop, vo.getAllShop())
                .set(MarketingLimitdiscount::getShopId, vo.getShopId())
                .eq(MarketingLimitdiscount::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = MarketingLimitdiscount.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

